package com.zh.note.huawei.onehundred.字符串统计or剩余可用字符集;

/**
 * https://dream.blog.csdn.net/article/details/129773642
 */

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        // String line = s.nextLine();
        String line = "a:3,b:5,c:2@a:1,b:2";
        solveMethod(line);
    }

    private static void solveMethod(String line) {
        String[] split = line.split("@");

        Map<String, Integer[]> map = new HashMap<>();

        String[] all = split[0].split(",");
        for (int i = 0; i < all.length; ++i) {
            String[] char_count = all[i].split(":");
            String c = char_count[0];
            int count = Integer.parseInt(char_count[1]);
            map.put(c, new Integer[]{i, count});
        }

        if (split.length > 1)
            for (String s : split[1].split(",")) {
                String[] char_count = s.split(":");
                String c = char_count[0];
                Integer[] item = map.get(c);
                item[1] -= Integer.parseInt(char_count[1]);
                map.put(c, item);
            }

        PriorityQueue<Map.Entry<String, Integer[]>> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(x -> x.getValue()[0]));
        // 把map放到优先队列里面
        for (Map.Entry<String, Integer[]> entry : map.entrySet()) {
            if (entry.getValue()[1] > 0) {
                priorityQueue.offer(entry);
            }
        }

        StringBuilder sb = new StringBuilder();

        while (!priorityQueue.isEmpty()) {
            Map.Entry<String, Integer[]> entry = priorityQueue.poll();
            sb.append(entry.getKey())
                    .append(":")
                    .append(entry.getValue()[1])
                    .append(",");
        }

        System.out.println(sb.substring(0, sb.length() - 1));
    }
}

